/*
 * Copyright (c) 2016. jiiiiiin 15398699939@163.com
 */

package com.bankscene.screenfit.adapters;

import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import com.bankscene.screenfit.model.Menus;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhaojin on 6/22/16.
 */

public class ViewPagerAdapter extends FragmentPagerAdapter {

    private final FragmentManager manager;
    private Logger log = LoggerFactory.getLogger(ViewPagerAdapter.class);
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
        this.manager = manager;
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;//需要销毁之前的菜单对应模块的fragments,替换成新的fragments
    }

    @Override
    public long getItemId(int position) {
        long res = mFragmentList.get(position).getClass().hashCode();
        log.debug("getItemId {}",res);
        return res;
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }

    public static ViewPagerAdapter newInstance(@NonNull Menus chooseMenu, FragmentManager supportFragmentManager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(supportFragmentManager);
        Menus[] fragmentNames = chooseMenu.getFragments();
        for (Menus menus : fragmentNames) {
            try {
                Fragment fragment = (Fragment) Class.forName(menus.getClassName()).newInstance();
                adapter.addFragment(fragment, menus.getTitle());
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }

        }
        return adapter;
    }

    public void updateBy(Menus chooseMenu) {
        mFragmentList.clear();
        mFragmentTitleList.clear();
        Menus[] fragmentNames = chooseMenu.getFragments();
        for (Menus menus : fragmentNames) {
            log.debug("menus {}",menus);
            try {
                Fragment fragment = (Fragment) Class.forName(menus.getClassName()).newInstance();
                addFragment(fragment, menus.getTitle());
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }

        }
        notifyDataSetChanged();
    }
}
